<?php

/**
 * This is the model class for table "{{good_data}}".
 *
 * The followings are the available columns in table '{{good_data}}':
 * @property string $id
 * @property integer $good_id
 * @property string $image
 * @property string $title
 * @property string $description
 * @property integer $create_time
 * @property integer $update_time
 * @property integer $article_id
 * @property string $article_token
 * @property integer $is_show
 * @property integer $listorder
 * @property integer $city_id
 * @property string $type
 * @property integer $is_image
 */
class GoodData extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{good_data}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('good_id, image, title, create_time, article_id, city_id', 'required'),
			array('good_id, create_time, update_time, article_id, is_show, listorder, city_id, is_image', 'numerical', 'integerOnly'=>true),
			array('image', 'length', 'max'=>120),
			array('title', 'length', 'max'=>100),
			array('article_token, type', 'length', 'max'=>20),
			array('description', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, good_id, image, title, description, create_time, update_time, article_id, article_token, is_show, listorder, city_id, type, is_image', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'good_id' => 'Good',
			'image' => 'Image',
			'title' => 'Title',
			'description' => 'Description',
			'create_time' => 'Create Time',
			'update_time' => 'Update Time',
			'article_id' => 'Article',
			'article_token' => 'Article Token',
			'is_show' => 'Is Show',
			'listorder' => 'Listorder',
			'city_id' => 'City',
			'type' => 'Type',
			'is_image' => 'Is Image',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('good_id',$this->good_id);
		$criteria->compare('image',$this->image,true);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('create_time',$this->create_time);
		$criteria->compare('update_time',$this->update_time);
		$criteria->compare('article_id',$this->article_id);
		$criteria->compare('article_token',$this->article_token,true);
		$criteria->compare('is_show',$this->is_show);
		$criteria->compare('listorder',$this->listorder);
		$criteria->compare('city_id',$this->city_id);
		$criteria->compare('type',$this->type,true);
		$criteria->compare('is_image',$this->is_image);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return GoodData the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
    public function getList($city_id,$good_id){
        $criteria = new CDbCriteria;
        $criteria->addCondition("city_id=$city_id");
        $criteria->addCondition("good_id=$good_id");
        $criteria->order = "id DESC";
        $dataProvider= new CActiveDataProvider($this, array(
            'criteria' => $criteria,
            'pagination'=>array(
                'pageSize'=>20,
            ),
        ));
        return $dataProvider;
    }
    public function getSearchList($city_id,$keyword,$good_id){
        $criteria = new CDbCriteria;
        $criteria->addCondition("city_id=$city_id");
        $criteria->addCondition("good_id=$good_id");
        $criteria->addSearchCondition('title',$keyword,true,'AND');
        $criteria->order = "id DESC";
        $dataProvider= new CActiveDataProvider($this, array(
            'criteria' => $criteria,
            'pagination'=>array(
                'pageSize'=>20,
            ),
        ));
        return $dataProvider;
    }
    public function getFrontCountList($good_id){
        $db = Yii::app()->db->createCommand();
        $db->select('count(*) as count');
        $db->from('{{good_data}}');
        $db->andWhere('is_show=:is_show and good_id=:good_id',array(':good_id'=>$good_id,':is_show'=>1));
        $info = $db->queryRow();
        return $info['count'];
    }
    public function getFrontList($good_id,$first,$total){
        $db = Yii::app()->db->createCommand();
        $db->select('id,image,title,description,article_token,type');
        $db->from('{{good_data}}');
        $db->andWhere('is_show=:is_show and good_id=:good_id',array(':good_id'=>$good_id,':is_show'=>1));
        $db->order('listorder desc,update_time desc');
        $db->limit($total,$first);
        $info = $db->queryAll();
        return $info;
    }
    public function getImageList($good_id){
        $db = Yii::app()->db->createCommand();
        $db->select('id,image,title,description,article_token,type');
        $db->from('{{good_data}}');
        $db->andWhere('is_show=:is_show and is_image=:is_image and good_id=:good_id',array(':is_image'=>1,':good_id'=>$good_id,':is_show'=>1));
        $db->order('listorder desc,update_time desc');
        $info = $db->queryAll();
        return $info;
    }
}
